;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Purchase.SchemaPriceSearch", [])
        .controller("SchemaPriceSearchCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$timeout", "$filter", "UserContextService", "$q", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $timeout, $filter, UserContextService, $q) {
            $scope.handleMultiInput = function(e) {
                $scope.PriceStock.condition.materialsNos = UserContextService.MultiInput(e.target.value);
            };

            $scope.PriceStock = {
                markInfo: {},
                pageInfo: {
                    totalPages: 1,
                    totalRows: 0,
                    pageSize: 10,
                    curPage: 1
                },
                dataList: [],
                condition: {},
                toolbarConfig: {
                    showAdvanceSearch: false,
                },
                tableConfig: {
                    scrollWidth: 800,
                    tableCol: [
                        {
                            name: "零件编号",
                            dataField: "materialsNo",
                            width: 150,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            name: "零件中文名",
                            dataField: "materialsDesc",
                            width: 300,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            name: "方案名称",
                            dataField: "activityName",
                            width: 200,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            name: "价格",
                            dataField: "activityPriceShow",
                            width: 150,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            name: "标准交期",
                            dataField: "standDeliveryTime",
                            width: 150,
                            isShow: true,
                            isFixed: false,
                        },
                    ]
                },
                search: function (curpage) {
                    getPriceStockList(curpage);
                },
                reset: function () {
                    $scope.PriceStock.condition = {};
                },
                switchToolbar: function (toolbar) {
                    if (!!$scope.PriceStock.toolbarConfig["show" + toolbar]) {
                        $scope.PriceStock.toolbarConfig["show" + toolbar] = false;
                    } else {
                        $timeout(function () {
                            $scope.PriceStock.toolbarConfig["show" + toolbar] = true;
                        });
                    }
                },
                selectRow: function (index) {
                    $scope.PriceStock.markInfo.curRow = index
                },
            };

            function getPriceStockList(curPage) {
                if (!$scope.PriceStock.condition.supplierOrgId) {
                    ybWidget.ShowToaster('error', '请选择工厂');
                    return;
                }
                var service = "device";
                var reqUrl = "/marketingActivity/supplierPriceScheme";
                if (!!curPage) {
                    $scope.PriceStock.pageInfo.curPage = curPage;
                }
                var param = {
                    pageSize: $scope.PriceStock.pageInfo.pageSize,
                    pageNum: $scope.PriceStock.pageInfo.curPage,
                    filters: {
                        ...$scope.PriceStock.condition,
                        jckFlag: $scope.isJCHK
                    }
                };
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.activityPriceShow = $filter("decimalCover")(item.activityPrice, 2);
                        item.isChecked = false
                    });
                    $scope.PriceStock.dataList = resp.value.list;
                    $scope.PriceStock.pageInfo.totalRows = resp.value.total;
                    $scope.PriceStock.pageInfo.totalPages = resp.value.pages;

                    $scope.PriceStock.markInfo = {
                        curRow: -1,
                        allCheckedBill: false
                    };
                });
            }

            function checkAll(list) {
                var selectAllFlag = false;
                for (var i = 0, len = list.length; i < len; i++) {
                    if (!list[i].isChecked) {
                        selectAllFlag = false;
                        break;
                    }
                    selectAllFlag = true;
                }
                return selectAllFlag;
            }

            $scope.selectAll = function (list, type) {
                angular.forEach(list, function (item) {
                    item.isChecked = $scope.PriceStock.markInfo["allChecked" + type];
                });
            };

            $scope.selectSingle = function (item, list, type) {
                if (!item.isChecked) {
                    $scope.PriceStock.markInfo["allChecked" + type] = false;
                } else {
                    $scope.PriceStock.markInfo["allChecked" + type] = checkAll(list);
                }
            };

            (function () {

            })();
        }])
    ;
})(window, window.angular);
